package com.wt.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.order.common.CustomException;
import com.wt.order.entity.Category;
import com.wt.order.entity.Dish;
import com.wt.order.entity.Setmeal;
import com.wt.order.mapper.CategoryMapper;
import com.wt.order.service.CategoryService;
import com.wt.order.service.DishService;
import com.wt.order.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 吴涛
 * @date 2022/5/10 16:47
 * @qq 337144203
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    //菜品
    @Autowired
    private DishService dishService;

    //套餐
    @Autowired
    private SetmealService setmealService;
    /**
     *根据id删除分类，删除之前需要判断是否关联了相应的菜品
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品，若有，就抛出一个业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int countDish = dishService.count(dishLambdaQueryWrapper);
        if (countDish > 0){
            //已经关联了菜品，抛出异常
        throw new CustomException("当前分类下已有菜品,请删除菜品后重试");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int countSetmeal = setmealService.count(setmealLambdaQueryWrapper);
        //查询当前分类是否关联了套餐，若有，就抛出一个业务异常
        if (countSetmeal > 0){
            //关联了套餐，不能被删除，抛出一个异常
            throw new CustomException("当前分类下已有套餐,请删除套餐后重试");
        }

        //正常删除
        super.removeById(id);

    }
}
